﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LifeSoft.BusinessObject;
using System.Data;
using System.Data.SqlClient;
using LifeSoftLibary;
namespace LifeSoft.DataLayer
{
    public class LoginData
    {
        SqlConnection conn = new SqlConnection(new ConnectUtils().SQL_CONNECTION_STRING);
        private SqlCommand cmd;
        private SqlDataReader reader;
        public long getUserLogin(String userName, String password)
        {
            long userId;
            try
            {
                string sql = "SELECT SYS_ACCOUNT_ID FROM SYS_ACCOUNT WHERE SYS_USER_NAME = '"+ userName +"' AND SYS_PASSWORD = '"+ password +"' ";

                cmd = new SqlCommand(sql, conn);
                conn.Open();
                if (cmd.ExecuteScalar() != null)
                {
                    Object obj = cmd.ExecuteScalar();
                    userId = long.Parse(obj.ToString());
                }
                else
                {
                    userId = 0L;
                }
                conn.Close();
            }
            catch (Exception)
            {
                throw;
            }
            return userId;
        }

        public SysLoginInfo  GLoginInfo (long sysUserId)
        {
            SysLoginInfo info = new SysLoginInfo();
            try
            {
                String sql = "SELECT * FROM V_USER WHERE SYS_ACCOUNT_ID = " + sysUserId;
                cmd = new SqlCommand(sql, conn);
                conn.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    info.userId = long.Parse(reader["SYS_USER_ID"].ToString());
                    info.lastName = (string) reader["LAST_NAME"];
                    info.firstName = (string) reader["FIRST_NAME"];
                    info.authorityName = (string) reader["SYS_AUTHORITY_NAME"];
                    info.groupName = (string) reader["SYS_GROUP_NAME"];
                    info.sysAccountId = long.Parse(reader["SYS_ACCOUNT_ID"].ToString());
                }                
                conn.Close();
            }
            catch (Exception)
            {
                throw;
            }
            return info;
        }
    }
}
